package links;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LinksHelper {
    static Map<String, Integer> cyMap;
    static MessageDigest digest;

    private static void init() throws IOException, NoSuchAlgorithmException {
        if (cyMap != null) {
            return;
        }
        digest = MessageDigest.getInstance("MD5");
        cyMap = new HashMap<String, Integer>();
        File folder = new File("cyimages");
        for (File file : folder.listFiles()) {
            int cy = Integer.valueOf(file.getName().split("\\.")[0]);
            InputStream fis = new FileInputStream(file);
            String md5 = getMD5(fis);
            cyMap.put(md5, cy);
        }
    }

    public static String getMD5(InputStream fis) throws IOException, NoSuchAlgorithmException {
        byte[] buffer = new byte[1024];
        int numRead;
        do {
            numRead = fis.read(buffer);
            if (numRead > 0) {
                digest.update(buffer, 0, numRead);
            }
        } while (numRead != -1);
        byte[] b = digest.digest();
        String md5 = "";
        for (int i = 0; i < b.length; i++) {
            md5 += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
        }
        return md5;
    }

    public static String getMD5(String str) throws IOException, NoSuchAlgorithmException {
        return getMD5(new ByteArrayInputStream(str.getBytes("UTF-8")));
    }

    public static int getCY(String link) throws IOException, NoSuchAlgorithmException {
        init();
        link = new URL(link).getHost();
        URLConnection conn = new URL("http://yandex.ru/cycounter?" + link).openConnection();
        conn.connect();
        String md5 = getMD5(conn.getInputStream());
        return cyMap.get(md5);
    }

    private static int countPR(String link) throws IOException, NoSuchAlgorithmException {
        String encodedLink = URLEncoder.encode(link, "UTF-8");
        long hash = new JenkinsHash().hash(("info:" + link).getBytes(), 0xE6359A60);
        String googleUrl = "http://toolbarqueries.google.com/search?client=navclient-auto&hl=en&ch=6" + hash + "&ie=UTF-8&oe=UTF-8&features=Rank&q=info:" + encodedLink;
        URLConnection conn = new URL(googleUrl).openConnection();
        conn.setReadTimeout(60000);
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        conn.connect();
        String line = reader.readLine();
        int pr = -1;
        if (line != null) {
            String[] fields = line.split(":");
            try {
                pr = Integer.valueOf(fields[2]);
            } catch (NumberFormatException e) {
                System.out.println(e.getMessage() + ": " + link);
            }
        }
        return pr;
    }

    public static String getPR(String link) throws IOException, NoSuchAlgorithmException {
        int domainPR = countPR(new URL(link).getHost());
        int linkPR = countPR(link);
        return domainPR + "|" + linkPR;
    }

    public static String getAlexa(String link) throws IOException {
        String alexaUrl = "http://www.alexa.com/siteinfo/" + link;
        Document doc = Jsoup.connect(alexaUrl).
                timeout(60000).get();
        Elements stats = doc.select("table#siteStats td div");
        String globalAlexa = stats.get(0).text();
        globalAlexa = globalAlexa.equals("") ? "NA" : globalAlexa;
        String localAlexa = stats.get(2).text();
        if (stats.size() < 9 || localAlexa.equals("")) {
            localAlexa = "NA";
        }
        String alexa = globalAlexa + "|" + localAlexa;
        return alexa;
    }

    public static final String YANDEX_CACHE_DATE_FORMAT = "dd.MM.yyyy";

    public static Date getYandexCache(String link) throws IOException, InterruptedException, ParseException {
        String yandexUrl = "http://yandex.ru/yandsearch?lr=1&text=";
        String linkwww;
        String linkNowww;
        String host = new URL(link).getHost();
        String path = new URL(link).getPath();
        if (host.contains("www.")) {
            linkwww = host + path;
        } else {
            linkwww = "www." + host + path;
        }
        linkNowww = linkwww.replace("www.", "");
        System.out.println(host);
        System.out.println(path);
        String text = URLEncoder.encode("(url:\"" + linkwww + "\" | url:\"" + linkNowww + "\")", "UTF-8");
        System.out.println(text);
        Document doc = Jsoup.connect(yandexUrl + text).
                header("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1").
                        header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8").
                        header("Cookie", "\tyandexuid=8152674981313160225; yp=1629384478.sp.nd:50:lang:!#1629099548.udn.dG9ueWdsb3ZhdHNreQ==; fuid01=4d726ae12b1a1ac0.N9hJY3OOtWG-kEtar5NvfvMUs1Xo_8AUqbE-fG1uUYhwf2PZq1e_rMNyTIPcAliV5rOj5oRQaNMCW7Z2qn2HXaeA_Otjy40U2yeL3hFKkrbBAo6y88nb2eM2jOvZk-KQ; yabs-frequency=/3/_m843rG98NUn01rN2Jm0007-S04TImad0001_iC35KW9dErm0We_2Lm000AKBW0bEmay0001Tm805p09l9Mj0mqm2Pm0/; my=YwECAAAnAgAALQECNgEBAA==; L=ckBGUX1YRGZYAnZIXXd4YARJRndUV398aAE8PAR+cl4aLSg4UEwvUU40FmMTG2sIJicuKBcvDAccQCQCQVgUEg==.1313739548.9085.282501.0b5effe4b2b7bb7852144b5680bb146f; aw=1_teJxiNGVgM9jmqLAiz4j53xZBjg/TzJgDtygK3NjiwHxtu6JAxF4H5lWbFAVa#XgPTnzYoCdw45MP8HAAAA//8DAO5oFDwA#A#; yandex_login=tonyglovatsky; Session_id=1313739548.-107.3.125472376.2:355607797:330.8:1313739548947:3162909315:22.34:355607796.77732.75875.81909d0ae39277b30d3e9a9af5943ac4; t=p; spravka=dD0xMzEzOTMyMTEzO2k9MTA5LjIwNS4yNTMuMTM1O3U9MTMxMzkzMjExMzY5MzUzOTE4NDtoPWU4NTNlNmExOWM0MWMzNDg1MWU5MjVmMzQ1NjZjZjIw").
                timeout(60000).get();
        if (!doc.hasClass("b-serp-item__links-link")) {
            return null;
        }
        Thread.sleep(200);
        String cacheLink = doc.select("a.b-serp-item__links-link").get(0).attr("href");
        doc = Jsoup.connect(cacheLink).
                header("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1").
                        header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8").
                        header("Cookie", "\tyandexuid=8152674981313160225; yp=1629384478.sp.nd:50:lang:!#1629099548.udn.dG9ueWdsb3ZhdHNreQ==; fuid01=4d726ae12b1a1ac0.N9hJY3OOtWG-kEtar5NvfvMUs1Xo_8AUqbE-fG1uUYhwf2PZq1e_rMNyTIPcAliV5rOj5oRQaNMCW7Z2qn2HXaeA_Otjy40U2yeL3hFKkrbBAo6y88nb2eM2jOvZk-KQ; yabs-frequency=/3/_m843rG98NUn01rN2Jm0007-S04TImad0001_iC35KW9dErm0We_2Lm000AKBW0bEmay0001Tm805p09l9Mj0mqm2Pm0/; my=YwECAAAnAgAALQECNgEBAA==; L=ckBGUX1YRGZYAnZIXXd4YARJRndUV398aAE8PAR+cl4aLSg4UEwvUU40FmMTG2sIJicuKBcvDAccQCQCQVgUEg==.1313739548.9085.282501.0b5effe4b2b7bb7852144b5680bb146f; aw=1_teJxiNGVgM9jmqLAiz4j53xZBjg/TzJgDtygK3NjiwHxtu6JAxF4H5lWbFAVa#XgPTnzYoCdw45MP8HAAAA//8DAO5oFDwA#A#; yandex_login=tonyglovatsky; Session_id=1313739548.-107.3.125472376.2:355607797:330.8:1313739548947:3162909315:22.34:355607796.77732.75875.81909d0ae39277b30d3e9a9af5943ac4; t=p; spravka=dD0xMzEzOTMyMTEzO2k9MTA5LjIwNS4yNTMuMTM1O3U9MTMxMzkzMjExMzY5MzUzOTE4NDtoPWU4NTNlNmExOWM0MWMzNDg1MWU5MjVmMzQ1NjZjZjIw").
                timeout(60000).get();
        Pattern p = Pattern.compile("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d");
        Matcher m = p.matcher(doc.toString());
        m.find();
        Date date = new SimpleDateFormat(YANDEX_CACHE_DATE_FORMAT).parse(doc.toString().substring(m.start(), m.end()));
        System.out.println(date);
        return date;
    }


    public static Date getGoogleCache(String link) throws IOException, InterruptedException, ParseException {
        String text = URLEncoder.encode("cache:" + link, "UTF-8");
        System.out.println(text);
//        String googleURL = "http://www.google.ru/search?hl=ru&source=hp&biw=1280&bih=566&q="+phrase+"&aq=1&aqi=g10&aql=&oq=&start=" + page;
        String googleURL = "http://webcache.googleusercontent.com/search?num=100&hl=en&newwindow=1&biw=1173&bih=540&q="+ text + "&btnG=Search";
        Document    doc = Jsoup.connect(googleURL).
                    header("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0").
//                        header("Cookie", "PREF=ID=ba43d045ae8fb44a:U=fdcd58c3e7ae19f9:FF=0:LD=ru:NR=100:NW=1:TM=1299345317:LM=1307794433:SG=2:S=yc6UGpylNNGBMS8u; NID=47=NXohthQxpzoceXA-JXJRptGBQc_cnRJzLeauru_HvnBJsAXNXmhR8wNyQQEah_4sNGKasyxbwo5XPUN2Av-dIef7W_M7x9wC9ECZqlxHqa6VdE2YqCqyXoZ3m8F1_sb4; HSID=ABJdKxI-fIR4G3ewf; SID=DQAAALgAAACmHoYtrB5YP9X1PkxWaYGz3b9U0x_16eTbhiw7mtpBWr061NkKWRggBgAQdX0Zr6oJMwu-UuEyMcS7F2D0ljLcsCr5hVrVFy-51EZ9E-OjcetgxB-DvR1BsLqMH_sNxchsf9mP93G1Tqkp3wBd8uTPdxX7Gc3HNdCkZMLR-f6OV4bSMhwofIcdMfBFZDBBjdByKwHwxfC9vABPAtj_6sF9muN7DHfHmr3VIOkOz19v0g2vMZ6TvHY4hpTTZXRye7E").
//                        header("Cookie", "PREF=ID=c97245adb651c3f1:U=6d413b3ee2e7906a:FF=0:LD=ru:NR=100:NW=1:TM=1310708899:LM=1310709588:SG=2:S=jbp3-3a3hM0IE4yK; NID=49=Si9QSOyeHWfTgafH0KwJXDDBTS51a40oYFqc8vR6NodZUXVzzgP1E9a4tMpPgX4O7AefjlHSiB6ZdVdb4rzXMEMfa1Wn4ZSZbmFapbU_KCCiFZAHrd7S2CoqF3N0V4YT").
//                        header("Cookie", "PREF=ID=c97245adb651c3f1:U=8e41e5555efd037e:FF=0:LD=ru:NR=100:NW=1:TM=1310708899:LM=1311449949:SG=2:S=Q6GBL80BtdnGFpHC; NID=49=kOhybtq4QnAPvH2Z5vsbceBbMfzGaHV-BUyOf2ckj9A1wrSifyDu7sL-kiV7qBb2ewQmeRnoQvWb13A6vRPeUpsb7yueYrCHlWpL_FdmJ-2HKR-8tQ3Oc2q_moas9HVF; SID=DQAAALgAAAANH_OsVDZ3CeDAk_RJ9qG18AJZh0E7wK3aGJ6trbCrVMXXS95UwCDR8IFS8WfjIHAIKgBwAmCq7_Z0ei4I6LTQaX8SV74rMhE3lTPyn9qP2gFJQ6EKUAJ6ywPuRN0Gu2BI_NfEuE2lf_TB6q7rCBV0uNPcSfuJpJbuYBil4S46Z48XElBJzoFmmsx9Hj4J2pv7Q_N2TrX7M82simX-AT5yb3EO8QANBxi9k6vsSYm9dYcb8K1tC0Hn3QVg3wKdOOw; HSID=AIaJT_RqE8PMALoi2; GDSESS=ID=7b014a0e39e38050:EX=1311460586:S=ADSvE-f_znc_rb6wExdGWHX_xao5phTIyg").
                    header("Cookie", "t=p; yandexuid=3271890011313154616; spravka=dD0xMzEzMTU0NjI3O2k9MTA5LjIwNS4yNTMuMTM1O3U9MTMxMzE1NDYyNzU0Mzk1MTkxNDtoPWIxZjY2ZjA5YmQzZjMyYmNmYTIwNGFhMTdmMzhmNjk2; yp=1629378627.sp.; fuid01=4d726ae12b1a1ac0.N9hJY3OOtWG-kEtar5NvfvMUs1Xo_8AUqbE-fG1uUYhwf2PZq1e_rMNyTIPcAliV5rOj5oRQaNMCW7Z2qn2HXaeA_Otjy40U2yeL3hFKkrbBAo6y88nb2eM2jOvZk-KQ").
                    timeout(60000).get();
        Pattern p = Pattern.compile("\\d\\d\\s\\d\\d\\d\\d");
        Matcher m = p.matcher(doc.toString());
        m.find();
        Date date = new SimpleDateFormat(YANDEX_CACHE_DATE_FORMAT).parse(doc.toString().substring(m.start(), m.end()));
        System.out.println(date);
        return date;
    }
}
